package pl.kech.pszczyna.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import pl.kech.pszczyna.domain.Gallery;
import pl.kech.pszczyna.domain.Image;
import pl.kech.pszczyna.repository.ImageDAO;


@Controller
@SessionAttributes(value = {"count"})
public class MainController {

	protected final Log logger = LogFactory.getLog(getClass());
	
	@PersistenceContext
	private EntityManager em;
	
	@Autowired
	private ImageDAO imageDao;

	@RequestMapping(value = "/")
	public String welcome(Model model) throws Exception {
		return "invite";
	}
	
	@Transactional
	@RequestMapping(value = "/gallery_cat")
	public ModelAndView getGalleryCat(Model model) throws Exception {
		String ejbqlString = "select gc from Gallery gc";
		@SuppressWarnings("unchecked")
		List<Gallery> galleryCatList = 
			em.createQuery(ejbqlString).getResultList();
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("command", new Gallery());
		modelMap.put("galleryCatList", galleryCatList);
		return new ModelAndView("gallery_cat", modelMap);
	}
	
	@SuppressWarnings("unchecked")
	@Transactional
	@RequestMapping(value = "/gallery/{galleryCatId}")
	public ModelAndView getGallery(@PathVariable Long galleryCatId, Model model) throws Exception {
		Gallery gallery = em.find(Gallery.class, galleryCatId);
		Query query = em.createNamedQuery(Image.GET_BY_CAT_ID).setParameter(Image.CATEGORY_ID, galleryCatId);
		List<Image> images = query.getResultList();
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("images", images);
		modelMap.put("gallery", gallery);
		return new ModelAndView("gallery", modelMap);
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/gallery_cat")
	public String putGalleryCat(@ModelAttribute Gallery galleryCat) {
		logger.info("post galleryCategory " + galleryCat);
		em.persist(galleryCat);
		return "redirect:/gallery_cat";
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/gallery/{galleryCatId}")
	public String uploadImage(@PathVariable Long galleryCatId, HttpServletRequest request) {
		imageDao.uploadImage(galleryCatId, request);
		return "redirect:/gallery/" + galleryCatId;
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/blob/{blobKeyStr}")
	public void getImage(@PathVariable String blobKeyStr, HttpServletResponse response) throws IOException {
		imageDao.getImage(blobKeyStr, response);
	}
	
	@Transactional
	@RequestMapping(method = RequestMethod.POST, value = "/image/{imageId}")
	public String deleteImage(@PathVariable Long imageId) throws IOException {
		Long galleryCatId = imageDao.deleteAndReturnGalleryCatId(imageId);
		return "redirect:/gallery/" + galleryCatId;
	}
	
}
